package debux.apps.mltr.app;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import debux.apps.mltr.model.DatabaseController;
import debux.apps.mltr.model.SongInfo;

public class SongActivity extends Activity {
	SongListAdapter adapter;
	ArrayList<SongInfo> songList;
	ListView songListView;
	EditText editTextSearch;
	ImageView imageSearchIcon;
	Handler handle = new Handler();
	@Override
	public void onCreate(Bundle bundle){
		super.onCreate(bundle);
		setContentView(R.layout.song_tab_frame);
		songList = new ArrayList<SongInfo>();
		DatabaseController.getAllSongs(songList);
		songListView = (ListView)findViewById(R.id.songListView);
		adapter = new SongListAdapter(this, R.layout.album_list_item, songList);
		songListView.setAdapter(adapter);
		songListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SongActivity.this,SongDetailsAcitivity.class);
				SongDetailsAcitivity.songInfo = songList.get(arg2);
				startActivity(intent);
			}
		});
		imageSearchIcon = (ImageView) findViewById(R.id.imageViewSearchIcon);
		editTextSearch = (EditText)findViewById(R.id.editTextSearch);
//		((InputMethodManager) getSystemService(this.INPUT_METHOD_SERVICE))  
//        .hideSoftInputFromWindow(editTextSearch.getWindowToken(), 0);
//		imageSearchIcon.requestFocus();
		editTextSearch.addTextChangedListener(new TextWatcher() {

			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {
				// TODO Auto-generated method stub
				String currentContent = editTextSearch.getText().toString();
				DatabaseController.searchAllSongsByName(currentContent,songList);
				handle.post(new Runnable() {
					
					@Override
					public void run() {
						// TODO Auto-generated method stub
						adapter.notifyDataSetChanged();
					}
				});
				if (currentContent.length()!=0){
					imageSearchIcon.setImageResource(R.drawable.delete_search_icon);
				} else {
					imageSearchIcon.setImageResource(R.drawable.search_icon);
				}
			}
			
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count,
					int after) {
				// TODO Auto-generated method stub
				
			}
			
			@Override
			public void afterTextChanged(Editable s) {
				// TODO Auto-generated method stub
				
			}
		});
		imageSearchIcon.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				editTextSearch.setText("");
			}
		});
	}
	
	class SongListAdapter extends ArrayAdapter<SongInfo>{
		ArrayList<SongInfo> songs;
		
		public SongListAdapter(Context context, int textViewResourceId,
				ArrayList<SongInfo> objects) {
			super(context, textViewResourceId, objects);
			// TODO Auto-generated constructor stub
			songs = objects;
		}
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View v = convertView;
            if (v == null) {
                LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vi.inflate(R.layout.album_list_item, null);
            }
            //ImageView albumThumb = (ImageView)v.findViewById(R.id.album_thumb);
            TextView albumName = (TextView)v.findViewById(R.id.album_name);
            TextView albumReleased = (TextView)v.findViewById(R.id.album_released);
            albumName.setText(songs.get(position).getName());
            albumReleased.setText(songs.get(position).getLength());
            return v;
		}
	}	
}
